import { apiClient } from './api-client';
import { 
  CarInfo, 
  CarQueryParams, 
  CarRequest, 
  BaseResponse,
  PaginationResponse 
} from '../types/api.types';

export class CarService {
  private static readonly BASE_URL = '/cars';

  /**
   * 获取车辆列表（分页）
   * @param params 查询参数
   * @returns 车辆列表
   */
  static async getCarList(params: CarQueryParams): Promise<BaseResponse<PaginationResponse<CarInfo>>> {
    try {
      const response = await apiClient.get<PaginationResponse<CarInfo>>(
        `${this.BASE_URL}/list`,
        params
      );
      return response;
    } catch (error) {
      console.error('获取车辆列表失败:', error);
      throw error;
    }
  }

  /**
   * 获取车辆详情
   * @param carId 车辆ID
   * @returns 车辆信息
   */
  static async getCarDetail(carId: number): Promise<BaseResponse<CarInfo>> {
    try {
      // 这里假设有获取车辆详情的接口
      const response = await apiClient.get<CarInfo>(
        `${this.BASE_URL}/${carId}`
      );
      return response;
    } catch (error) {
      console.error('获取车辆详情失败:', error);
      throw error;
    }
  }

  /**
   * 创建车辆
   * @param carData 车辆数据
   * @returns 创建结果
   */
  static async createCar(carData: CarRequest): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.post<string>(
        this.BASE_URL,
        carData
      );
      return response;
    } catch (error) {
      console.error('创建车辆失败:', error);
      throw error;
    }
  }

  /**
   * 更新车辆信息
   * @param carData 车辆数据
   * @returns 更新结果
   */
  static async updateCar(carData: CarRequest): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.put<string>(
        this.BASE_URL,
        carData
      );
      return response;
    } catch (error) {
      console.error('更新车辆失败:', error);
      throw error;
    }
  }

  /**
   * 删除车辆
   * @param carId 车辆ID
   * @returns 删除结果
   */
  static async deleteCar(carId: number): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.delete<string>(
        `${this.BASE_URL}/${carId}`
      );
      return response;
    } catch (error) {
      console.error('删除车辆失败:', error);
      throw error;
    }
  }

  /**
   * 批量删除车辆
   * @param carIds 车辆ID数组
   * @returns 批量删除结果
   */
  static async batchDeleteCars(carIds: number[]): Promise<BaseResponse<string>> {
    try {
      const response = await apiClient.delete<string>(
        `${this.BASE_URL}/batch`,
        { body: carIds }
      );
      return response;
    } catch (error) {
      console.error('批量删除车辆失败:', error);
      throw error;
    }
  }

  /**
   * 更新车辆状态
   * @param carId 车辆ID
   * @param status 状态
   * @returns 更新结果
   */
  static async updateCarStatus(carId: number, status: number): Promise<BaseResponse<string>> {
    try {
      // 这里假设有更新状态的接口
      const response = await apiClient.patch<string>(
        `${this.BASE_URL}/${carId}/status`,
        { status }
      );
      return response;
    } catch (error) {
      console.error('更新车辆状态失败:', error);
      throw error;
    }
  }

  /**
   * 搜索车辆
   * @param keyword 关键词
   * @returns 搜索结果
   */
  static async searchCars(keyword: string): Promise<BaseResponse<CarInfo[]>> {
    try {
      // 这里假设有搜索接口，或者使用列表接口进行搜索
      const response = await apiClient.get<CarInfo[]>(
        `${this.BASE_URL}/search`,
        { keyword }
      );
      return response;
    } catch (error) {
      console.error('搜索车辆失败:', error);
      throw error;
    }
  }

  /**
   * 获取车辆统计信息
   * @returns 统计信息
   */
  static async getCarStatistics(): Promise<BaseResponse<{
    total: number;
    onSale: number;
    sold: number;
    offSale: number;
  }>> {
    try {
      // 这里假设有统计接口
      const response = await apiClient.get<{
        total: number;
        onSale: number;
        sold: number;
        offSale: number;
      }>(`${this.BASE_URL}/statistics`);
      return response;
    } catch (error) {
      console.error('获取车辆统计失败:', error);
      throw error;
    }
  }

  /**
   * 导出车辆数据
   * @param params 查询参数
   * @returns 导出文件URL
   */
  static async exportCars(params: CarQueryParams): Promise<BaseResponse<{ url: string }>> {
    try {
      const response = await apiClient.post<{ url: string }>(
        `${this.BASE_URL}/export`,
        params
      );
      return response;
    } catch (error) {
      console.error('导出车辆数据失败:', error);
      throw error;
    }
  }
}

export default CarService;